/*******************************************************************************
        
        copyright:		Copyright (C) Mike Reinhold 2010. All rights reserved
        				
        license:		BSD style: $(LICENSE)
        		
        author(s): 		Mike Reinhold.
        				
*******************************************************************************/
module tango.util.cipher.pad.ANSIx923;

import tango.util.cipher.Cipher;

/** Implementation of the ANSI x.923 padding scheme */
class ANSIx923: BlockCipherPadding {

	ANSIx923 dup() {
		return new ANSIx923();
	}
	
	/***************************************************************************
	
			name
			
			Returns:
			The name of the padding scheme			
	
	***************************************************************************/
	string name() {
		return "ANSI X.923";
	}
	
	/***************************************************************************
	
			allowsReordering
			
			Returns:
			does the padding allow safe reordering?
			
			Remarks:
			See the classes in tango.util.cipher.order for further details. 

	***************************************************************************/
	bool allowsReordering(){
		return true;
	}

	/***************************************************************************
	
			pad
			
			Params:
			len = number of pad bytes to be generated
			
			Returns:
			a ubyte array containing the padding bytes

	***************************************************************************/
	ubyte[] pad(uint len) {
		auto pad = new ubyte[len]; 

		// set last pad byte to number of pad bytes
		if(len > 0)
			pad[len - 1] = cast(ubyte) len;

		return pad;
	}
	
	/***************************************************************************
	
			unpad
			
			Params:
			input_ = the padded text in which padding bytes should be detected
			
			Returns:
			the number of pad bytes.

	***************************************************************************/	
	uint unpad(void[] input_) {
		//last byte holds the number of pad bytes
		auto input = cast(ubyte[]) input_;
		return cast(uint) (input[input.length - 1]); // the number of pad bytes
	}
}